import Reflux from 'reflux';
import StateMixin from 'reflux-state-mixin';
import ServiceItemAction from 'pages/serviceitems/actions/serviceItemAction.js';
import API from 'config/api';
export default Reflux.createStore({
  mixins:[StateMixin.store],
  listenables:[ServiceItemAction],
  getInitialState() {
    return {
      items:[],
      srvItem: {},
      count: 1,
      users: []
    };
  },
  onGetAllUsers() {
    $.get(API.GET_ALLUSERS,(data) => {
      this.setState({
        users: data.data
      });
    });
  },
  onGetModelList() {
    $.ajax({
      url: API.GET_MODEL_LIST,
      method: 'get',
      contentType: 'application/json',
      datatype:'json'
    })
    .done((data) => {
      if (data.errCode === 200) {
        ServiceItemAction.getModelList.completed(data.data);
      }
    })
    .fail((error) => {
      ServiceItemAction.getModelList.failed(error);
    })
  },
  onDeletItems(data) {
    $.ajax({
      url: API.DELETE_SRVITEM,
      type: 'POST',
      data: JSON.stringify(data),
      contentType: 'application/json',
      datatype:'json'
    })
    .done((data) => {
      if (data.errCode === 200) {
        ServiceItemAction.deletItems.completed(data.data);
      }
    })
    .fail((error) => {
      ServiceItemAction.deletItems.failed(error);
    })
  },
  onChangeStatus(data) {
    $.ajax({
      url: API.CHANGE_STATUS + '?id=' + data.id + '&status=' + data.status,
      method: 'put',
      contentType: 'application/json',
      datatype:'json'
    })
    .done((data) => {
      if (data.errCode === 200) {
        ServiceItemAction.changeStatus.completed(data.data);
      }
    })
    .fail((error) => {
      ServiceItemAction.changeStatus.failed(error);
    })
  },
  onUpdateSrvItem(srvitem) {
    $.ajax({
      url: API.UPDATE_SRVITEM,
      method: 'put',
      data: JSON.stringify(srvitem),
      contentType: 'application/json',
      datatype:'json'
    })
    .done((data) => {
      if (data.errCode === 200) {
        ServiceItemAction.updateSrvItem.completed(data.data);
      }
    })
    .fail((error) => {
      ServiceItemAction.updateSrvItem.failed(error);
    })
  },
  onGetSrvItemById(id) {
    $.ajax({
      url: API.GET_SRVITEMBYID,
      method: 'get',
      data: id,
      datatype:'json'
    })
    .done((data) => {
      if (data.errCode === 200) {
        this.setState({
          srvItem:data.data
        })
        ServiceItemAction.getSrvItemById.completed(data.data);
      }
    })
    .fail((error) => {
      ServiceItemAction.getSrvItemById.failed(error);
    })
  },
  onGetSrvItemList(data) {
    $.get(API.GET_SRVITEMLIST, data ,(data) => {
      this.setState({
        items: data.data ? data.data.list : null,
        count: data.data ? data.data.count : 0
      });
    });
  },
  onCreateSrvItem(srvitem) {
    $.ajax({
      url: API.CREATE_SRVITEM,
      method: 'post',
      data: JSON.stringify(srvitem),
      contentType: 'application/json',
      datatype:'json'
    })
    .done((data) => {
      if (data.errCode === 200) {
        ServiceItemAction.createSrvItem.completed(data.data);
      }
    })
    .fail((error) => {
      ServiceItemAction.createSrvItem.failed(error);
    })
  },
  onGetSrvStandardList() {
    $.ajax({
      url: API.GET_STANDARDLIST,
      method: 'get',
      contentType: 'application/json',
      datatype:'json'
    })
    .done((data) => {
      if (data.errCode === 200) {
        ServiceItemAction.getSrvStandardList.completed(data.data);
      }
    })
    .fail((error) => {
      ServiceItemAction.getSrvStandardList.failed(error);
    })
  }
});

